package com.xw.lianshou.xuexi.iostream;

import org.springframework.core.io.FileSystemResource;

import java.io.*;

/**
 * @author xw
 * @date 2021/6/17 23:14
 */
public class FileTest {
    public static void main(String[] args) throws IOException {
        // 传入当前路径，获得的是项目根目录：E:\IdeaProjects\lianshou\.
        FileSystemResource resource = new FileSystemResource("./");
        System.out.println(resource.getFile().getAbsolutePath());

        // 传入根目录路径，获得的就是操作系统的根目录
        resource = new FileSystemResource("/");
        System.out.println(resource.getFile().getAbsolutePath());  // 输出 E:\

        // 获取单元测试resources目录下的test.txt，需要指定详细的路径
//        resource = new FileSystemResource("src/main/resources/application.properties");
        resource = new FileSystemResource("src/main/resources/recv.txt");
        final File file = resource.getFile();

        // 一次性读取所有文件内容
        // 获取文件的长度
        Long length = file.length();
        byte[] filecontent = new byte[length.intValue()];
        FileInputStream in = new FileInputStream(file);
        in.read(filecontent);
        in.close();

        String str = new String(filecontent,"utf-8");
//        String str = new String(filecontent,"gbk");
        System.out.println(str);
        // 分割
        String[] sss = str.split("\r\n");
        System.out.println(sss[0]);

       /* final BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        String line = null;

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }*/
    }

}
